package com.retch.wenchengmao.recodeauthutils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.retch.wenchengmao.recodeauthutils.fragment.FourFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.OneFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.ThreeFragment;
import com.retch.wenchengmao.recodeauthutils.fragment.TwoFragment;

/**
 * author:wenchengmao
 * time:  2017/3/27.
 * email: wenchengmao@yeah.net
 */
//底部导航栏
public class MainBottomFragmentActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar bottomNavigationBar;
    Fragment fragmentOne,fragmentTwo,fragmentThree,fragmentFour;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_bottom_fragment);
        initView();
    }

    //初始化控件
    public void initView()
    {
        bottomNavigationBar=(BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "one"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "two"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "three"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "four"))
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        setDefaultFragment();

    }

    public void setDefaultFragment()
    {

    }

    //切换Fragment

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = this.getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (fragmentOne == null) {
                    fragmentOne = OneFragment.newInstance("one");
                }
                transaction.replace(R.id.frame_layout, fragmentOne);
                break;
            case 1:
                if (fragmentTwo == null) {
                    fragmentTwo = TwoFragment.newInstance("two");
                }
                transaction.replace(R.id.frame_layout, fragmentTwo);
                break;
            case 2:
                if (fragmentThree == null) {
                    fragmentThree = ThreeFragment.newInstance("找医生");
                }
                transaction.replace(R.id.frame_layout, fragmentThree);
                break;
            case 3:
                if (fragmentFour == null) {
                    fragmentFour = FourFragment.newInstance("医药之友");
                }
                transaction.replace(R.id.frame_layout, fragmentFour);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
